package com.pm2.controller.workcalendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pm2.dao.WorkCalendar.WorkCalendarDao;
import com.pm2.dao.WorkCalendar.WorkCalendarYearMonthDao;
import com.pm2.entity.workCalendar.WorkCalendar;
import com.pm2.entity.workCalendar.WorkCalendarYearMonth;
import com.shuohe.util.returnBean.ReturnBean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Controller  
@RequestMapping("/pm2/workcalendar/*")  
public class WorkCalendarController {

	@Autowired
	private WorkCalendarDao workCalendarDao; 
	@Autowired
	private WorkCalendarYearMonthDao workCalendarYearMonthDao;
	
	@RequestMapping(value="initYear.do")  
	public @ResponseBody ReturnBean initYear(HttpServletRequest request, HttpServletResponse response) {
		
		String year = request.getParameter("year").replaceAll("\"\"", "null");
		WorkCalendarYearMonth check = workCalendarYearMonthDao.findByText(year+"年");
		if(check!= null) return new ReturnBean(false, "年份已存在");
		
		try {
			

			WorkCalendarYearMonth workCalendarYear = new WorkCalendarYearMonth();
			workCalendarYear.setPid(0);
			workCalendarYear.setText(year+"年");
			workCalendarYear = workCalendarYearMonthDao.save(workCalendarYear);
			
			for(int i=1;i<13;i++)
			{
				WorkCalendarYearMonth month = new WorkCalendarYearMonth();
				month.setPid(workCalendarYear.getId());
				month.setText(i+"月");
				month = workCalendarYearMonthDao.save(month);			
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dBegin = sdf.parse(year + "-"+i+"-01 23:59:59");
				Date dEnd = sdf.parse(year + "-"+i+"-31 23:59:59");
				System.out.print(year + "-"+i+"-01");
				System.out.print("------");
				System.out.println(year + "-"+i+"-31");
				List<Date> lDate = findDates(dBegin, dEnd);
				for (Date date : lDate) {
					WorkCalendar w = new WorkCalendar();
					Calendar ca = Calendar.getInstance();
					// 使用给定的 Date 设置此 Calendar 的时间
					ca.setTime(date);

					if (ca.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
							|| ca.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
						w.set_weekend(true);
					} else {
						w.set_weekend(false);
					}
					w.setPid(month.getId());
					w.setYear(ca.get(ca.YEAR));
					w.setMonth(ca.get(ca.MONTH)+1);
					w.setDay(ca.get(ca.DAY_OF_MONTH));
					w.set_holiday(false);
					w.setDay_date(date);
					w.setId(0);
					System.out.println(sdf.format(date));
					workCalendarDao.save(w);
				}
			}
			return new ReturnBean(true, "");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnBean(false, "添加年份日历错误");
		}
	}
	//JAVA获取某段时间内的所有日期 
	public static List<Date> findDates(Date dBegin, Date dEnd) {
		List lDate = new ArrayList();
		lDate.add(dBegin);
		Calendar calBegin = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		calBegin.setTime(dBegin);
		Calendar calEnd = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		calEnd.setTime(dEnd);
		// 测试此日期是否在指定日期之后
		while (dEnd.after(calBegin.getTime())) {
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			calBegin.add(Calendar.DAY_OF_MONTH, 1);
			lDate.add(calBegin.getTime());
		}
		return lDate;
	}
	@RequestMapping(value="getList.do")  
	public @ResponseBody List<WorkCalendarYearMonth> getList(HttpServletRequest request, HttpServletResponse response) {
		return getMenuList(0);
	}
	
	public List<WorkCalendarYearMonth> getMenuList(int parent_id) {
		// TODO Auto-generated method stub
		 List<WorkCalendarYearMonth> list = workCalendarYearMonthDao.findAll();
		 return getChildsManyGroup(list,parent_id);
	}
	public List<WorkCalendarYearMonth> getChildsManyGroup(List<WorkCalendarYearMonth> list,int pid)
	{  
		List<WorkCalendarYearMonth> arr = new ArrayList<WorkCalendarYearMonth>();  
		for(WorkCalendarYearMonth location : list){  
		    if(pid == location.getPid()){  
		        location.setChildren(getChildsManyGroup(list, location.getId()));    
		        arr.add(location);   
		    }  
		}  
		return arr;  
	}
	
	
	@RequestMapping(value="setHoliday.do")  
	public @ResponseBody ReturnBean setHoliday(@RequestParam("id") int id,@RequestParam("is_holiday") boolean is_holiday) {
		WorkCalendar workCalendar = workCalendarDao.findById(id);
		if(workCalendar == null) return new ReturnBean(false,"不存在这个日期");
		
		workCalendar.set_holiday(is_holiday);
		workCalendarDao.save(workCalendar);
		 return new ReturnBean(true,"修复成功");
	}
	
	
	

}
